package edu.hawaii.duedates.configuration;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Holds all the configuration data from the default XML configuration
 * file located in /.duedates/duedates.xml. 
 * 
 * @author tylerwolff
 *
 */
public class DueDatesConfiguration implements Serializable {

  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  
  /** The smtp server name to use for mailing. */
  private String smtpServer;
  
  /** A list of Users defined in the XML file. */
  private final List<User> users = new ArrayList<User>();
  
  /**
   * Sets the smtp server name for the given configuration.
   * 
   * @param smtpServer The name of the smtp server to use.
   */
  public void setSmtpServer (String smtpServer) {
    this.smtpServer = smtpServer;
  }
  
  /**
   * Gets and returns the string representation of the SMTP server 
   * name.
   * @return The SMTP server name.
   */
  public String getSmtpServer () {
    return this.smtpServer;
  }
  
  /**
   * Adds a new user to the list of users in the configuration file.
   * 
   * @param newUser A new user to add to the list.
   */
  public void addUser(User newUser) {
    this.users.add(newUser);
  }
  
  /**
   * Goes through the user list and returns the user with the same email
   * and password. Used for the login page of the DueDates web app. Returns a null 
   * User object if no such user exists.
   * 
   * @param email The email to search User by.
   * @param password The password of the user.
   * @return The user with specified email.
   */
  public User validateUser (String email, String password) {

    for (User user : this.users) {
      if (user.getEmail().equals(email) && user.getPassword().equals(password)) {
        return user;
      }
    }
    
    // If no users exist with given email and password, return null
    return null;
  }
}
